package com.stylefeng.guns.rest.common.aop;

import com.stylefeng.guns.common.bo.WebuserBo;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.util.WebUserKit;
import com.stylefeng.guns.core.exception.GunsException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
@Order(100)//优先级低一点
public class NeedRealNameAop {

    @Pointcut(value = "@annotation(com.stylefeng.guns.rest.common.aop.NeedRealName)")
    public void controllerAspect() {
    }

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) {
        WebuserBo webuserBo = WebUserKit.getWebuserBo();
        if (webuserBo == null) {
            throw new GunsException(BizExceptionEnum.USER_NOT_LOGIN);
        }
        if (!webuserBo.getHasRealName()) {//未实名
            if (!webuserBo.getHasRealNameApplyFor()) {
                throw new GunsException(BizExceptionEnum.USER_NEED_REALNAME_APPLY_FOR);
            }
            throw new GunsException(BizExceptionEnum.USER_NEED_REALNAME_AUDIT);
        }
    }

}
